package components
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class User{
		private var panel:SuperPanel = new SuperPanel();
		private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var labelUsername:Label = new Label();
		private var labelTip:Label = new Label();
		private var labelLastLogin:Label = new Label();
		private var labelLastLogInTime:Label = new Label();
		private var labelLastLogInDate:Label = new Label();
		private var labelIme:Label = new Label();
		private var labelSmjer:Label = new Label();
		private var labelBrojIndeksa:Label = new Label();
		private var avatar:Image = new Image();
		private var buttonSend:Button = new Button();
		private var tool:Tool = new Tool();
		
		private var _id:String = "";
		private var _username:String = "";
		public function User(p:SuperPanel){
			panel = p;
			labelUsername.x = 10;
			labelUsername.y = 27;
			labelUsername.width = panel.width-30;
			labelUsername.height = 25;
			labelTip.x = 10;
			labelTip.y = 47;
			labelTip.width = panel.width-30;
			labelTip.height = 25;
			labelLastLogin.x = 10;
			labelLastLogin.y = 67;
			labelLastLogin.width = panel.width-30;
			labelLastLogin.height = 25;
			labelLastLogInTime.x = 30;
			labelLastLogInTime.y = 87;
			labelLastLogInTime.width = panel.width-30;
			labelLastLogInTime.height = 25;
			labelLastLogInDate.x = 30;
			labelLastLogInDate.y = 107;
			labelLastLogInDate.width = panel.width-30;
			labelLastLogInDate.height = 25;
			labelIme.x = 10;
			labelIme.y = 137;
			labelIme.width = panel.width-30;
			labelIme.height = 25;
			labelSmjer.x = 10;
			labelSmjer.y = 157;
			labelSmjer.width = panel.width-30;
			labelSmjer.height = 25;
			labelBrojIndeksa.x = 10;
			labelBrojIndeksa.y = 177;
			labelBrojIndeksa.width = 150;
			labelBrojIndeksa.height = 25;
			avatar.width = 100;
			avatar.height = 100;
			avatar.y = 27;
			avatar.x = panel.width - 125;
			buttonSend.x = panel.width - 150;
			buttonSend.y = panel.height - 40;
			buttonSend.label = "POSALJI PORUKU";
			buttonSend.height = 20;
			buttonSend.addEventListener(MouseEvent.CLICK, onSendMessage);
			
			panel.addChild(avatar);
			panel.addChild(labelUsername);
			panel.addChild(labelTip);
			panel.addChild(labelLastLogInTime);
			panel.addChild(labelLastLogInDate);
			panel.addChild(labelLastLogin);
			panel.addChild(buttonSend);
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
		}

		public function onSendMessage(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist("NOVA PORUKA")){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("NOVA PORUKA", 0, 400, 400, 50, 50, true, false, "NovaPoruka"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				var n:NewMessage = new NewMessage(s);
				n.defaultPrimaoc = _username;
			}
		}

		public function onResize(e:ResizeEvent):void{
			labelUsername.width = panel.width-30;
			labelTip.width = panel.width-30;
			labelLastLogin.width = panel.width-30;
			labelLastLogInTime.width = panel.width-30;
			labelLastLogInDate.width = panel.width-30;
			labelIme.width = panel.width-30;
			labelSmjer.width = panel.width-30;
			labelBrojIndeksa.width = 150;
			avatar.x = panel.width - 125;
			buttonSend.x = panel.width - 150;
			buttonSend.y = panel.height - 40;
		}
		
		public function set id(x:String):void{
			_id = x;
			service.sql = {type:"SELECT", a1:"k.id_username", a2:"k.username", a3:"k.tip", a4:"k.avatar", a5:"last_login_date", a6:"last_login_time", from:"korisnik k", where:"k.id_username="+id };
	 		service.addEventListener(service.MYSQL_RESULT, onKorisnikResult);
	 		service.startService();
		}
		
		public function onKorisnikResult(e:Event):void{
			_username = service.xml.result.username;
			labelUsername.htmlText = "Korisnicko ime: <b>"+service.xml.result.username+"</b>";
			var tip:String;
			if(service.xml.result.tip==0) {
				tip="Administrator";
			} else if(service.xml.result.tip==1) {
				tip="Student";
				service1.sql = {type:"SELECT", a1:"s.ime", a2:"s.prezime", a3:"s.brojIndeksa", a4:"m.naziv", from:"student s, smjer m", where:"s.smjer=m.id_smjer AND s.username="+service.xml.result.id_username };
	 			service1.addEventListener(service1.MYSQL_RESULT, onStudentResult);
	 			service1.startService();
			} else if(service.xml.result.tip==2) {
				tip="Predavac";
				service1.sql = {type:"SELECT", a1:"p.ime", a2:"p.prezime", from:"predavac p", where:"p.username="+service.xml.result.id_username };
	 			service1.addEventListener(service1.MYSQL_RESULT, onPredavacResult);
	 			service1.startService();
			} 
			labelTip.htmlText = "Tip korisnika: <b>"+tip+"</b>";
			labelLastLogin.htmlText = "Zadnja aktivnost:"
			labelLastLogInDate.htmlText = "- Datum:  <b>"+service.xml.result.last_login_date+"</b>";
			labelLastLogInTime.htmlText = "- Vrijeme: <b>"+service.xml.result.last_login_time+"</b>";
			avatar.source=(service.xml.result.avatar!="") ? panel.parentApplication.AVATAR_PATH+service.xml.result.avatar : panel.parentApplication.AVATAR_PATH+"nophoto.jpg";
		}
		
		public function onStudentResult(e:Event):void{
			labelIme.htmlText = "Ime i prezime: <b>"+service1.xml.result.ime+" "+service1.xml.result.prezime+"</b>";
			labelSmjer.htmlText = "Smjer: <b>"+service1.xml.result.naziv+"</b>";
			labelBrojIndeksa.htmlText = "Broj indeksa: <b>"+service1.xml.result.brojIndeksa+"</b>";
			panel.addChild(labelIme);
			panel.addChild(labelSmjer);
			panel.addChild(labelBrojIndeksa);
		}
		
		public function onPredavacResult(e:Event):void{
			labelIme.htmlText = "Ime i prezime: <b>"+service1.xml.result.ime+" "+service1.xml.result.prezime+"</b>";
			panel.addChild(labelIme);
		}
		
		public function get id():String{
			return _id;
		}
	}
}